/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourpkr.view;

import java.awt.Dimension;
import java.awt.geom.AffineTransform;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.plaf.DimensionUIResource;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.*;
import org.apache.batik.util.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;

/**
 *
 * @author nb888
 */
public class carteFactori {

    Document carteMap = null;
    String uri;

    public carteFactori(String uri) {
        this.uri = uri;
    }

    public Vector<aCarte> fact(int nb) {
        Vector<aCarte> res = new Vector();
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        try {
            //carteMap = f.createDocument(uri);
            for (int i = 0; i < nb; i++) {
                res.add(new aCarte((SVGDocument) f.createDocument(uri), 2632.5, 1575.0));
            }
        } catch (Exception e) {
        }
        return res;
    }

    public void test() {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        try {
            carteMap = f.createDocument(uri);
        } catch (Exception e) {
        }
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        SVGDocument t = (SVGDocument) carteMap;

        Document firstCart = SVGDOMImplementation.getDOMImplementation().createDocument(svgNS, "svg", null);
        NamedNodeMap n = t.getRootElement().getAttributes();
        for (int i = 0; i < n.getLength(); i++) {
            firstCart.getDocumentElement().setAttribute(n.item(i).getNodeName(), n.item(i).getTextContent());
        }
        Node toImport;
        String fam = "diamond";
        for (int i = 1; i < 10; i++) {
            toImport = t.getRootElement().getElementById(i + "_" + fam);
            firstCart.getFirstChild().appendChild(firstCart.importNode(toImport, true));
        }
        toImport = t.getRootElement().getElementById("1_club");
        firstCart.getFirstChild().appendChild(firstCart.importNode(toImport, true));
        //dump(firstCart);
        show(carteMap);
    }

    public void dump(Document doc) {
        dumpNode(doc.getFirstChild(), 0);
    }

    public void dumpNode(Node n, int tab) {
        for (int i = 0; i < tab; i++) {
            System.out.print("  ");
        }
        System.out.print("<" + n.getNodeName() + n.getTextContent() + ">\n");
        if (n.hasAttributes()) {
            for (int j = 0; j < n.getAttributes().getLength(); j++) {
                for (int i = 0; i < tab; i++) {
                    System.out.print("  ");
                }
                System.out.print(n.getAttributes().item(j).getNodeName() + "=" + n.getAttributes().item(j).getTextContent() + "\n");
            }
        }
        for (int i = 0; i < n.getChildNodes().getLength(); i++) {
            dumpNode(n.getChildNodes().item(i), tab + 1);
        }
        for (int i = 0; i < tab; i++) {
            System.out.print("  ");
        }
        System.out.print("</" + n.getNodeName() + ">\n");
    }

    public void show(Document doc) {
        JFrame fr = new JFrame();
        final JSVGCanvas canvas = new JSVGCanvas();
        final double zoomFact = 1;
        double W = 2632.5;
        double H = 1575;
        double cW = W / 13;
        double cH = H / 5;
        int cartX = (int) Math.round(cW * zoomFact);
        int cartY = (int) Math.round(cH * zoomFact);
        Dimension windowsDim = new Dimension(cartX, cartY);
        fr.setSize(windowsDim);
        fr.setMaximumSize(windowsDim);
        fr.setPreferredSize(windowsDim);
        fr.getContentPane().add(canvas);
        //canvas.setEnableImageZoomInteractor(true);
        canvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {

            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
            // Build Started
            }

            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                // Build Done
                canvas.setRenderingTransform(AffineTransform.getScaleInstance(zoomFact, zoomFact));
            }
        });
        canvas.setSVGDocument((SVGDocument) doc);
        fr.pack();
        fr.setVisible(true);
        canvas.setSize((int) W, (int) H);
        canvas.setLocation(-cartX * 2, -cartY);
    }

    public static void main(String[] args) {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ourpkr.OurPkrApp.class).getContext().getResourceMap(Joueur.class);
        carteFactori fact = new carteFactori(Joueur.class.getResource("/" + resourceMap.getResourcesDir() + resourceMap.getString("carteFile")).toString());
        Vector<aCarte> cartes = fact.fact(2);
        JFrame fr = new JFrame();
        for (int i = 0; i < cartes.size(); i++) {
            fr.getContentPane().add(cartes.elementAt(i));
        }
        fr.pack();
        fr.setVisible(true);
        cartes.get(0).setColor(2);
        cartes.get(0).setVal(3);
    }
}


